package com.roy.workbench.bookmark.service;



import com.roy.workbench.bookmark.domain.Bookmark;
import com.roy.workbench.bookmark.domain.BookmarkRepository;
import com.roy.workbench.bookmark.domain.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.awt.print.Book;
import java.util.List;


/**
 * Created by elloxxn on 12/6/2017.
 */
@Service
public class BookmarkService {
    @Autowired
    private BookmarkRepository repository;

    public Page<Bookmark> query(Pageable pageable) {
        return repository.findAll(pageable);
    }

    public Bookmark findById(Long id) {
        return repository.findOne(id);
    }

    public List<Bookmark> findByCatalogId(Long catalogId) {
        Catalog c = new Catalog();
        c.setId(catalogId);
        Bookmark b = new Bookmark();
        b.setCatalog(c);
        return repository.findAll(Example.of(b));
    }

    public Bookmark save(Bookmark bookmark) {
        return repository.saveAndFlush(bookmark);
    }

    public Bookmark delete(Long id, Boolean needReturn) {
        if (needReturn) {
            Bookmark bookmark = repository.findOne(id);
            repository.delete(id);
            return bookmark;
        }
        return null;
    }

}
